import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { AddVMSService } from 'app/pages/dissemination-management/message-library/add-vms/add-vms.service';
import { DisseminationService } from 'app/pages/dissemination-management/dissemination-management.service';
@Component({
    selector: 'itmp-add-vms-message-pictogram',
    templateUrl: './pictogram.component.html',
    styleUrls: ['./pictogram.component.css']
})
export class PictogramComponent implements OnInit, OnDestroy {
    search$ = new Subject<string>();
    listOfData: any[] = [];
    keyword = null;
    subscripions: Subscription[] = [];
    constructor(public addService: AddVMSService, private diss: DisseminationService) {}

    ngOnInit() {
        this.searchPipeSet();
        this.getData();
    }
    ngOnDestroy() {
        this.subscripions.forEach(item => {
            item.unsubscribe();
        });
    }
    getData() {
        this.keyword = this.keyword === '' ? null : this.keyword;
        const params = {
            condition: {
                graphic_id: null,
                graphic_name: this.keyword,
                graphic_type: null,
                height: null,
                url: null,
                width: null
            },
            pageNum: 1,
            pageSize: 1000000
        };
        this.diss.getGraphicList(params).subscribe(res => {
            const content = res.data;
            if (!content) {
                this.listOfData = [];
                return;
            }
            this.listOfData = content.content;
        });
    }
    // 防抖函数注册
    searchPipeSet() {
        const subscripion = this.search$.pipe(debounceTime(1000), distinctUntilChanged()).subscribe((kw: any) => {
            this.getData();
        });
        this.subscripions.push(subscripion);
    }
    // 搜索(防抖)
    onSearch(): void {
        this.search$.next(this.keyword);
    }
    clickTR(data) {
        this.addService.setGraphics(data);
    }
    // drop(event: CdkDragDrop<{ title: string, poster: string }[]>) {
    //     moveItemInArray(this.listOfData, event.previousIndex, event.currentIndex);
    // }
}
